package com.virtualightning.lib

import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

fun main() {

    val dispatcher = object : CoroutineDispatcher() {
        override fun dispatch(context: CoroutineContext, block: Runnable) {
            block.run()
        }
    }


    val scope2 = object: CoroutineScope {
        override val coroutineContext: CoroutineContext
            get() = EmptyCoroutineContext
    }

    var num = 0

    val deferred1 = scope2.async(dispatcher) {
        delay(1000)
        println("1 Run on ${Thread.currentThread()}")
        for(i in 0 until 1000) {
            delay(1)
            num ++
        }
    }

    val deferred2 = scope2.async(dispatcher) {
        delay(1000)
        println("2 Run on ${Thread.currentThread()}")
        for(i in 0 until 1000) {
            delay(1)
            num ++
        }
    }

    val deferred3 = scope2.async(dispatcher) {
        delay(1000)
        println("3 Run on ${Thread.currentThread()}")
        for(i in 0 until 1000) {
            delay(1)
            num ++
        }
    }

    GlobalScope.launch {
        deferred1.await()
        deferred2.await()
        deferred3.await()

        println("result: $num")
    }

    Thread.sleep(10000)
}

public class MyClass {

}